package com.wudimeng.syll.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.dao.ICChuKuXinXiDAO;
import com.wudimeng.syll.dao.ICRuKuXinXiDAO;
import com.wudimeng.syll.dao.IYongHuXinXiDAO;
import com.wudimeng.syll.entity.CChuKuXinXi;
import com.wudimeng.syll.entity.CRuKuXinXi;
import com.wudimeng.syll.entity.CRuKuXinXiShanChu;
import com.wudimeng.syll.entity.YongHuXinXi;
import com.wudimeng.syll.service.ICRuKuXinXiService;
import com.wudimeng.syll.service.IYongHuXinXiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class CRuKuXinXiService implements ICRuKuXinXiService {

    @Autowired
    private ICRuKuXinXiDAO dao;

    @Autowired
    private ICChuKuXinXiDAO chukudao;

    @Override
    public ApiResponse selectCRuKuXinXi(Map<String, String> paramMap, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> maps = dao.selectCRuKuXinXi(paramMap);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);
//        return ApiResponse.instance(pageInfo);
        return ApiResponse.instance(dao.selectCRuKuXinXi(paramMap));
    }

    @Override
    @Transactional
    public ApiResponse addCRuKuXinXi(CRuKuXinXi ruKuXinXi) {
        try {
            Integer lengKuID = ruKuXinXi.getLengKuID();
            String chuKuLiuShuiHao = ruKuXinXi.getChuKuLiuShuiHao();
            if(!StringUtils.isBlank(chuKuLiuShuiHao)){
                //查询出库信息
                Map<String, Object> chuKuXinXi = chukudao.selectCChuKuXinXiByLiuShuiHao(chuKuLiuShuiHao,null);
                Integer liuSHuiID = (Integer)chuKuXinXi.get("LiuSHuiID");
                CChuKuXinXi chuKu = new CChuKuXinXi();
                chuKu.setLiuSHuiID(liuSHuiID);
                chuKu.setIs_RK("是");
                chukudao.updCChuKuXinXi(chuKu);
            }
            //追溯码
            String lengKuXinXi = dao.getLengKuXinXi(lengKuID);
            if(StringUtils.isBlank(lengKuXinXi)){
                return ApiResponse.error("未查询到冷库区域！");
            }
            String zhuiSuMa = "21" + lengKuXinXi.substring(3,5) + this.getNumberForPK();
            ruKuXinXi.setZhuiSuMa(zhuiSuMa);
            dao.addCRuKuXinXi(ruKuXinXi);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_INSERT_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_INSERT_SUCCESS);
    }

    @Override
    public ApiResponse updCRuKuXinXi(CRuKuXinXi ruKuXinXi) {
        try {
            CRuKuXinXi cRuKuXinXi = dao.selectCRuKuXinXiByLiuShuiID(ruKuXinXi.getLiuSHuiID().toString());
//            int i = dao.getRuKuXinXiShenHeByLiuShuiHao(cRuKuXinXi.getLiuShuiHao());
//            if(i > 0){
//                return ApiResponse.error("审核已通过不允许修改！");
//            }
            GregorianCalendar calendar = new GregorianCalendar();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour >= 17){
                return ApiResponse.error("超过17点不允许删除！");
            }
            //判断是否可以删除  是否有下一级出库单
            Map<String, Object> chuKuXinXi = chukudao.getChuKuXinXiByRuKuLiuShuiId(ruKuXinXi.getLiuSHuiID().toString());
            if(chuKuXinXi != null && chuKuXinXi.size() > 0){
                return ApiResponse.error("下游出库单已产生不允许删除！");
            }
            dao.updCRuKuXinXi(ruKuXinXi);
            return ApiResponse.error(Constant.COMMON_UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    public ApiResponse delCRuKuXinXi(String liuShuiID) {
        try {
            CRuKuXinXi ruKuXinXi = dao.selectCRuKuXinXiByLiuShuiID(liuShuiID);
            int i = dao.getRuKuXinXiShenHeByLiuShuiHao(ruKuXinXi.getLiuShuiHao());
            if(i > 0){
                return ApiResponse.error("审核已通过不允许修改！");
            }
            GregorianCalendar calendar = new GregorianCalendar();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour >= 17){
                return ApiResponse.error("超过17点不允许删除！");
            }
            //判断是否可以删除  是否有下一级出库单
            Map<String, Object> xinXi = chukudao.getChuKuXinXiByRuKuLiuShuiId(liuShuiID);
            if(xinXi != null && xinXi.size() > 0){
                return ApiResponse.error("下游出库单已产生不允许删除！");
            }
            String chuKuLiuShuiHao = ruKuXinXi.getChuKuLiuShuiHao();
            if(!StringUtils.isBlank(chuKuLiuShuiHao)){
                //查询出库信息
                Map<String, Object> chuKuXinXi = chukudao.selectCChuKuXinXiByLiuShuiHao(chuKuLiuShuiHao,null);
                Integer liuSHuiID = (Integer)chuKuXinXi.get("LiuSHuiID");
                CChuKuXinXi chuKu = new CChuKuXinXi();
                chuKu.setLiuSHuiID(liuSHuiID);
                chuKu.setIs_RK("否");
                chukudao.updCChuKuXinXi(chuKu);
            }
            dao.addCRuKuXinXiShanChu(ruKuXinXi);
            dao.delCRuKuXinXi(liuShuiID);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_DELETE_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_DELETE_SUCCESS);
    }

    @Override
    public ApiResponse selectCRuKuXinXiByLiuShuiHao(String liuShuiHao,String liuShuiID) {
        Map<String, Object> map = new HashMap<>();
        map = dao.selectCRuKuXinXiByLiuShuiHao(liuShuiHao,liuShuiID);
        return ApiResponse.instance(map);
    }

    @Override
    public ApiResponse getTypeByLiuShuiHao(String liuShuiHao) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> map = chukudao.selectCChuKuXinXiByLiuShuiHao(liuShuiHao,null);
        if (map == null || map.size() == 0) {
            return ApiResponse.error("未查询到信息");
        } else {
            String guoChanHuoJinKouChuKu = map.get("GuoChanHuoJinKouChuKu").toString();
            if ("国产出库".equals(guoChanHuoJinKouChuKu)) {
                resultMap.put("ifJinKou", 0);
            } else {
                resultMap.put("ifJinKou", 1);
            }
            resultMap.put("type", "chuku");
            return ApiResponse.instance(resultMap);
        }
    }

    @Override
    public ApiResponse getRuKuLiuShuiHao(Map<String,String> map) {
        List<Map<String, Object>> ruKuLiuShuiHao = dao.getRuKuLiuShuiHao(map);
        if(ruKuLiuShuiHao == null || ruKuLiuShuiHao.size() == 0){
            return ApiResponse.error("未查询到信息");
        }
        return ApiResponse.instance(ruKuLiuShuiHao);
    }

    @Override
    public ApiResponse liuShuiHaoZhuiSu(String liuShuiHao) {
        String templiuShuiHao = liuShuiHao;
        List<Map<String,String>> resultList = new ArrayList<>();
        while (!StringUtils.isBlank(templiuShuiHao)){
            Map<String, String> chuKuXinXi = dao.findChuKuXinXiByLiuShuiHao(templiuShuiHao);
            if(chuKuXinXi != null && chuKuXinXi.size() >0){
                resultList.add(chuKuXinXi);
                templiuShuiHao = chuKuXinXi.get("RuKuLiuShuiHao");
                Map<String, String> ruKuXinXi = dao.findRuKuXinXiByLiuShuiHao(templiuShuiHao);
                if(ruKuXinXi != null && ruKuXinXi.size() >0) {
                    resultList.add(ruKuXinXi);
                    templiuShuiHao = ruKuXinXi.get("ChuKuLiuShuiHao");
                }else{
                    templiuShuiHao = null;
                    continue;
                }
            }else{
                templiuShuiHao = null;
                continue;
            }
        }
        return ApiResponse.instance(resultList);
    }

    public static String getNumberForPK(){
        String id="";
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String temp = sf.format(new Date());
        int random=(int) (Math.random()*1000000);
        id=temp+random;
        return id;
    }

    public static void main(String[] args) {
        System.out.println("21" + "ALL06".substring(3,5) + getNumberForPK());
    }

}
